package com.henghai.zhenxuan.ui.page.raise;

import static com.henghai.zhenxuan.App.getOpenSDK;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.henghai.zhenxuan.BR;
import com.henghai.zhenxuan.ui.yingshi.EZRealPlayActivity;
import com.henghai.zhenxuan.ui.yingshi.SdkInitParams;
import com.henghai.zhenxuan.ui.yingshi.SdkInitTool;
import com.henghai.zhenxuan.ui.yingshi.ServerAreasEnum;
import com.henghai.zhenxuan.utils.EZUtils;
import com.henghai.zhenxuan.R;
import com.kunminx.architecture.ui.page.BaseActivity;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.page.StateHolder;
import com.videogo.constant.IntentConsts;
import com.videogo.exception.BaseException;
import com.videogo.openapi.bean.EZCameraInfo;
import com.videogo.openapi.bean.EZDeviceInfo;

import java.util.List;

public class MonitorActivity extends BaseActivity {
    private HomeStates states;

    // token登录参数
    public static SdkInitParams mInitParams;
    // 开发者账号信息
    private final static String APPKEY_DEV = "67cf2f06edc14f86999968afe23bff7c";
    private final static String TOKEN_DEV = "at.aq5skvqb31rqum9g6rtuopvoae3wm0iu-2h8nua90xx-002vfjw-gbkbpa3a5";

    private Button btnStartPlay;
    private Button btnStopPlay;

    private static final int REQUEST_CODE_PERMISSIONS = 1001;
    private static final String[] REQUIRED_PERMISSIONS = {
            Manifest.permission.INTERNET,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE
    };

    private static final String TAG = "MonitorActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initUI();
        checkPermissions();
    }

    /**
     * 通过AccessToken/tkToken进行体验
     */
    public void onClickStartExperience(View view) {
        ServerAreasEnum mCurrentServerArea = ServerAreasEnum.getAllServers().get(0);
        SdkInitParams sdkInitParams = SdkInitParams.createBy(mCurrentServerArea);
        sdkInitParams.appKey = "67cf2f06edc14f86999968afe23bff7c";
        sdkInitParams.token = "at.1qkq3vs3dcpk0xmc3fgytddwcj2405k6-4g4uq5hbzy-0le7ucn-aovbpnum8";
        sdkInitParams.openApiServer = mInitParams.openApiServer;
        sdkInitParams.openAuthApiServer = mInitParams.openAuthApiServer;
//        sdkInitParams.openApiServer ="https://open.ys7.com";
//        sdkInitParams.openAuthApiServer = "https://openauth.ys7.com";
        sdkInitParams.specifiedDevice = "";
        SdkInitTool.initSdk(getApplication(), sdkInitParams);
        Asan asan = new Asan();
        asan.execute();
    }

    public class  Asan extends AsyncTask<Void, Void, List<EZDeviceInfo>>{

        @Override
        protected List<EZDeviceInfo> doInBackground(Void... voids) {
            List<EZDeviceInfo> result = null;
            EZDeviceInfo info = null;
            try {
                info =   getOpenSDK().getDeviceInfo("BG3970259");
//                result = getOpenSDK().getDeviceList(0, 20);

//                EZDeviceInfo deviceInfo = result.get(0);
                final EZCameraInfo cameraInfo = EZUtils.getCameraInfoFromDevice(info, 0);
                if (cameraInfo != null) {
                    Intent intent = new Intent(MonitorActivity.this, EZRealPlayActivity.class);
                    intent.putExtra(IntentConsts.EXTRA_CAMERA_INFO, cameraInfo);
                    intent.putExtra(IntentConsts.EXTRA_DEVICE_INFO, info);
                    startActivity(intent);
//                    startActivityForResult(intent, REQUEST_CODE);
                }

            } catch (BaseException e) {
                throw new RuntimeException(e);
            }
            Log.d(TAG, "onClickStartExperience: " + info.getDeviceSerial());
            return result;
        }
    }
    private void initUI() {
        btnStartPlay = findViewById(com.henghai.zhenxuan.R.id.btn_start_play);
        btnStopPlay = findViewById(R.id.btn_stop_play);

        btnStartPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickStartExperience(v);
            }
        });

        btnStopPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        btnStopPlay.setEnabled(false); // 初始时停止按钮不可用
    }

    private void initData() {
        loadDefaultSdkInitParams();
        SdkInitTool.initSdk(getApplication(), mInitParams);
    }

    /**
     * 设置默认参数
     */
    private void loadDefaultSdkInitParams() {
        mInitParams = SdkInitParams.createBy(null);
        mInitParams.appKey = APPKEY_DEV;
        mInitParams.openApiServer = "https://open.ys7.com";
        mInitParams.openAuthApiServer = "https://openauth.ys7.com";
    }

    private void checkPermissions() {
        // 检查并请求权限
        boolean allPermissionsGranted = true;
        for (String permission : REQUIRED_PERMISSIONS) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                allPermissionsGranted = false;
                break;
            }
        }

        if (!allPermissionsGranted) {
            ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS);
        } else {
        }
    }


    @Override
    protected void initViewModel() {
        states = getActivityScopeViewModel(HomeStates.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.fragement_monitr, BR.vm, states);
    }


    public static class HomeStates extends StateHolder {

    }
}
